/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package CastleDefense.units;

import CastleDefense.Animation;
import CastleDefense.Enemy;
import CastleDefense.Game;
import CastleDefense.Managers.GameManager;
import CastleDefense.Managers.StateManager;
import CastleDefense.Managers.UnitManager;
import CastleDefense.Managers.UpgradeManager;
import java.awt.Image;
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
import javax.vecmath.Vector2d;

/**
 *
 * @author Firedemon
 */
public class UnitTrap extends PlacedUnit
{
    private Animation trapAnimation;
    private boolean   attachedRock;
    private boolean   tntActive;
    private int       rocks;
    private int       lastWave = 0;
    
    private Timer     timer = new Timer();

    public UnitTrap(Vector2d loc, Image img, String name)
    {
        super(loc, img, name);

        trapAnimation = new Animation(getAnimation(name), 5);
        lastWave      = GameManager.getInstance().getCurrentWave();
        this.setAnimation(trapAnimation);
        this.resetRocks();
    }

    @Override
    public void doLogic()
    {
        if(this.getName().equals("Spikes"))
        {
            double trapDamage        = UpgradeManager.getInstance().getCurrentDamage(this);
            ArrayList<Enemy> enemies = Game.getEnemyList();

            for (Enemy effected : enemies)
            {
                if(this.collidesWith(effected) && !effected.getTrapEffected().contains(this) && this.damagesTarget(effected))
                {
                    effected.addTrapEffector(this);
                    trapAnimation.start();
                    effected.doDamageCalculation(trapDamage, this);
                    if(effected.isDead())
                        trapAnimation.stop();
                }
            }
        }
        else if(this.getName().equals("Death Spikes"))
        {
            int trapLevel = UnitManager.getInstance().getTrapLevel();
            ArrayList<Enemy> enemies = Game.getEnemyList();

            for (Enemy e : enemies)
            {
                if(this.collidesWith(e) && !e.getTrapEffected().contains(this) && this.damagesTarget(e))
                {
                    double enemyHP = e.getHP();

                    e.addTrapEffector(this);

                    if(this.killsTarget(trapLevel))
                    {
                        trapAnimation.start();
                        e.doDamageCalculation(enemyHP,this);
                        if(e.isDead())
                            trapAnimation.stop();
                    }
                }
            }
        }
        else if(this.getName().equals("Catapult"))
        {           
            //if use started a new wave reset the net count
            if(lastWave != GameManager.getInstance().getCurrentWave())
            {
                resetRocks();
                lastWave = GameManager.getInstance().getCurrentWave();
            }            

        }
        else if (this.getName().equals("TNT"))
        {
            if(tntActive)
                return;
            
            trapAnimation.start();
            
            TimerTask task = new TimerTask()
            {
                @Override
                public void run()
                {
                    doTNTLogic();
                }
            };
 
            timer.schedule(task, 3000);
        }
    }
    
    private void doTNTLogic()
    {        
        tntActive = true;
        
        trapAnimation.stop();
        
        timer.cancel();
        timer.purge();

        double trapDamage = UpgradeManager.getInstance().getCurrentDamage(this);
        List<Enemy> effectedList = StateManager.getInstance().getNearbyEnemies(this, 3);

        for (Enemy effected : effectedList)
            effected.doDamageCalculation((trapDamage), this);
    }
    
    public void setAttachedRock(boolean ar)
    {
        this.attachedRock = ar;
    }
    
    public boolean isRockAttached()
    {
        return attachedRock;
    }
    
    public void removeRock()
    {
        this.rocks--;
        if(rocks<0)
            rocks=0;
    }
    
    public boolean hasRocks()
    {
        return (rocks>0);
    }
    
    private void resetRocks()
    {
        this.rocks = UnitManager.getInstance().getTrapLevel();
    }
    
    public int getCurrentRocks()
    {
        return rocks;
    }
    
    public boolean isTNTActive()
    {
        return tntActive;
    }
}
